import log4js from 'log4js'

export enum LogLevel {
  TRACE = 'trace',
  DEBUG = 'debug',
  INFO = 'info',
  WARN = 'warn',
  ERROR = 'error',
  FATAL = 'fatal',
}

export class LogUtil {
  static logUtil: LogUtil = new LogUtil()
  private logInstance!: log4js.Logger
  private constructor() {
    this.config()
  }

  private config() {
    log4js.configure({
      appenders: {
        console: { type: 'console' },
        info_file: { type: 'file', filename: 'logger/info_log.log' },
      },
      categories: {
        default: {
          appenders: ['console'],
          level: LogLevel.DEBUG,
        },
        info: {
          appenders: ['console', 'info_file'],
          level: LogLevel.INFO,
        },
        warn: {
          appenders: ['console'],
          level: LogLevel.WARN,
        },
        error: {
          appenders: ['console'],
          level: LogLevel.ERROR,
        },
      },
    })
  }

  private getCategories(level: LogLevel) {
    this.logInstance = log4js.getLogger(level)
  }

  debug(msg: any, ...args: any[]) {
    this.getCategories(LogLevel.DEBUG)
    this.logInstance.debug(msg, ...args)
  }

  info(msg: any, ...args: any[]) {
    this.getCategories(LogLevel.INFO)
    this.logInstance.info(msg, ...args)
  }

  warn(msg: any, ...args: any[]) {
    this.getCategories(LogLevel.WARN)
    this.logInstance.warn(msg, ...args)
  }

  error(msg: any, ...args: any[]) {
    this.getCategories(LogLevel.ERROR)
    this.logInstance.error(msg, ...args)
  }
}

export default LogUtil.logUtil
